//
//  HJJVideoViewController.m
//  Pianke
//
//  Created by 黄俊杰 on 15/12/22.
//  Copyright © 2015年 dada. All rights reserved.
//

#import "HJJVideoViewController.h"
#import "WWYVideoModel.h"
#import "WWYCollectionVideoCell.h"
#import "WWYPlayerController.h"

@interface HJJVideoViewController ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate,WWYCollectionVideoCellDelegate>

@property (nonatomic, strong) UICollectionView *collection;

@property (nonatomic, strong) NSMutableArray *allArray;

@end

@implementation HJJVideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.allArray = [NSMutableArray array];
    //数据请求
    [LDJ_AppTools getUrl:@"http://baobab.wandoujia.com/api/v2/feed?num=2&udid=55d5b6ac32b8471d8486615407d3775114e7d66e&vc=64&vn=1.8.1&deviceModel=ASUS_X002&first_channel=eyepetizer_wandoujia_market&last_channel=eyepetizer_wandoujia_market" body:nil result:LDJJSON headerFile:nil success:^(id result) {
        NSMutableArray *temp = [result valueForKey:@"issueList"];
        
        for (NSDictionary *dic in temp) {
            for (NSDictionary *data in [dic valueForKey:@"itemList"]) {
                
                if ([[data valueForKey:@"type"] isEqualToString:@"video"]){
                    WWYVideoModel *model = [WWYVideoModel initWithDictionary:[data valueForKey:@"data"]];
                    [self.allArray addObject:model];
                }
            }
        }
        [self.collection reloadData];
        //创建旋转木马(有了之后, 不得不加菊花)
        [self makeCarousel];
    } failure:^(NSError *error) {
        NSLog(@"%@", error);
        
    }];

}

- (void)makeCarousel{
    WWYCollectionLayout *horizontal = [[WWYCollectionLayout alloc] initWithWWYSTYLE:WWYHorizontal];
    horizontal.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    horizontal.itemSize = CGSizeMake(SCREEN_WIDTH *  17 / 20 , SCREEN_HEIGHT * 7 / 10);
    //可视数(这个地方会有问题)
    horizontal.visibleCount = self.allArray.count;
    
    //Collection基本配置
    self.collection = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:horizontal];
    self.collection.delegate = self;
    self.collection.dataSource = self;
    
    //注册
    [self.collection registerClass:[WWYCollectionVideoCell  class] forCellWithReuseIdentifier:@"cell"];
    self.collection.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collection];
    
    //强加Masonry
    [self.collection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(25, 0, 20, 0));
    }];
}

//item点击事件
- (void)WWYCollectionVideoCellModel:(WWYVideoModel *)model{
    WWYPlayerController *hehe = [[WWYPlayerController alloc] init];
    [self presentViewController:hehe animated:YES completion:^{
    }];
    hehe.model = model;
}

//collectionView基本设置
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.allArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    NSString *identifer = @"cell";
    WWYCollectionVideoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifer forIndexPath:indexPath];
    cell.model = self.allArray[indexPath.row];
    cell.backgroundColor = [UIColor darkGrayColor];
    cell.delegate = self;
    return cell;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
